package com.kerrykidz.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.system.dao.mapper.SecRole2usrEntityMapper;
import com.kerrykidz.system.dao.mapper.SecUsrEntityMapper;
import com.kerrykidz.system.dao.mapper.WfmAuthorizeEntityMapper;
import com.kerrykidz.system.dao.model.SecRole2usrEntity;
import com.kerrykidz.system.dao.model.SecUsrEntity;
import com.kerrykidz.system.dao.model.WfmAuthorizeEntity;
import com.kerrykidz.system.service.IWfmAuthorizeService;
import com.kerrykidz.system.util.DataSourceUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.dao.mapper.ActivitiEntityMapper;
import com.kerrykidz.workflow.dao.model.ActivitiEntity;
import com.rdp.framework.exception.RDPException;

@Service
public class WfmAuthorizeServiceImpl implements IWfmAuthorizeService {

	@Autowired
	private WfmAuthorizeEntityMapper wfmAuthorizeEntityMapper;

	@Autowired
	private SecUsrEntityMapper secUsrEntityMapper;

	// 流程定义
	@Autowired
	private ActivitiEntityMapper activitiEntityMapper;

	@Autowired
	private SecRole2usrEntityMapper secRole2usrEntityMapper;
	
	@Autowired
	DataSourceUtil ds;

	@Override
	public WfmAuthorizeEntity findWfmAuthorize(WfmAuthorizeEntity wfmAuthorize)
			throws RDPException {
		wfmAuthorize.setIspaging(true);
		List<WfmAuthorizeEntity> beanList = wfmAuthorizeEntityMapper
				.findWfmAuthorize(wfmAuthorize);
		if (beanList.size() > 0) {
			wfmAuthorize.setResults(beanList);
		}
		return wfmAuthorize;
	}

	/**
	 * 获取委托人列表
	 * 
	 * @return
	 * @throws RDPException
	 */
	@Override
	public List<WfmAuthorizeEntity> findFromUsers(String fkBranId) throws RDPException {

		return wfmAuthorizeEntityMapper.findFromUsers(fkBranId);
	}

	/**
	 * 根据临时授权编号，更新delFlage，取消委托
	 * 
	 * @param wfmAuthorize
	 * @throws RDPException
	 */
	@Override
	public void cancelWfmAuthorize(String pkAuthId, String modiBy)
			throws RDPException {
		wfmAuthorizeEntityMapper.cancelWfmAuthorize(pkAuthId, modiBy);
	}

	/**
	 * 根据用户角色获取制定门店用户的详细信息
	 * 
	 * @param entity
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@Override
	public List<SecUsrEntity> selectUsesJobInfoByRole(SecUsrEntity entity)
			throws RDPException {
		return secUsrEntityMapper.selectUsesJobInfoByRole(entity);
	}

	/**
	 * 获取所有的流程定义
	 * 
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月19日
	 */
	@Override
	public List<ActivitiEntity> queryProcDef() throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		List<ActivitiEntity> list = ds.queryProcDef(user.getBranId());
		if(null == list)
			list = activitiEntityMapper.queryProcDef();
		return list;
	}

	/**
	 * 根据用户id获取用户角色
	 * 
	 * @param userId
	 * @return
	 * @throws RDPException
	 */
	@Override
	public List<SecRole2usrEntity> findUserRoleById(String fkUsrId)
			throws RDPException {
		return secRole2usrEntityMapper.findUserRoleById(fkUsrId);
	}

	/**
	 * 新增新的审批转授权
	 * 
	 * @param wfmAuthorize
	 * @throws RDPException
	 */
	@Override
	public void insertWfmAuthorize(WfmAuthorizeEntity wfmAuthorize)
			throws RDPException {
		wfmAuthorizeEntityMapper.insertWfmAuthorize(wfmAuthorize);
	}
}
